package com.company.cloud.behavior.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.behavior.modules.api.response.BehaviorSearchParam;
import com.company.cloud.behavior.modules.api.service.BehaviorClientService;
import com.company.cloud.behavior.modules.app.controller.req.FollowQuery4PageParam;
import com.company.cloud.behavior.modules.app.controller.req.ScoreQuery4PageParam;
import com.company.cloud.behavior.modules.app.controller.req.UserScoreSaveParam;
import com.company.cloud.behavior.modules.app.controller.resp.UserScoreDetailModel;
import com.company.cloud.behavior.modules.app.service.UserScoreService;
import com.company.cloud.behavior.modules.base.entity.UserScoreInfo;
import com.company.cloud.behavior.modules.base.service.UserScoreInfoService;
import com.company.cloud.behavior.modules.cache.models.ScoreItemModel;
import com.company.cloud.behavior.modules.cache.models.UserScoreModel;
import com.company.cloud.behavior.modules.cache.service.BehaviorCacheService;
import com.company.cloud.behavior.modules.cache.utils.BehaviorConvertUtils;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.function.Function;

/**
 * 点赞
 */
@Login
@RestController
@RequestMapping("/app/behavior/score")
@Api(tags="App_Behavior_评分服务")
public class ScoreController {

	@Autowired
	private UserScoreInfoService userScoreInfoService;

	@Autowired
	private BehaviorCacheService behaviorCacheService;

	@Autowired
	private UserScoreService userScoreService;

	@Qualifier("behaviorClientServiceImpl")
	@Autowired
	private BehaviorClientService behaviorClientService;

	@PostMapping("/getUserScore")
	@ApiOperation(value="获取对象评分情况", response=UserScoreDetailModel.class)
	public BaseResult getUserScore(@RequestBody FollowQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		UserScoreDetailModel model = new UserScoreDetailModel();
		model.setObjNo(params.getObjNo());
		model.setObjType(params.getObjType());
		BehaviorSearchParam param = new BehaviorSearchParam();
		BeanUtils.copyProperties(params, param);
		param.setUserId(userInfo.getId());
		model.setScore(behaviorClientService.getScoreAvgInfo(param).getData());
		model.setUserScore(behaviorClientService.getUserScoreSearchModel(param).getData());
		model.setScoreItem(behaviorCacheService.getScoreItem(params.getObjType()));
		return BaseResult.ok(model);
	}

	@PostMapping("/query")
	@ApiOperation(value="我的评分列表查询", response=UserScoreModel.class)
	public BaseResult query(@RequestBody ScoreQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<UserScoreInfo> page = userScoreInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<UserScoreInfo>()
						.eq("user_id", userInfo.getId())
						.eq(CheckUtils.hasText(params.getObjType()), "obj_type", params.getObjType())
						.eq(CheckUtils.hasText(params.getObjNo()), "obj_no", params.getObjNo())
						.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
						.orderByDesc("cdt")
		);

		IPage<UserScoreModel> data = page.convert(new Function<UserScoreInfo, UserScoreModel>() {
			@Override
			public UserScoreModel apply(UserScoreInfo entity) {
				return BehaviorConvertUtils.setUserScoreModel(entity);
			}
		});

		return BaseResult.ok(data);
	}

	@GetMapping("/queryScoreItem")
	@ApiOperation(value="评分项列表", response= ScoreItemModel.class)
	public BaseResult queryScoreItem(@RequestParam("objType")String objType) {
		return BaseResult.ok(behaviorCacheService.getScoreItem(objType));
	}

	@PostMapping("/save")
	@ApiOperation(value="新增")
	public BaseResult save(@RequestBody UserScoreSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		return BaseResult.ok(userScoreService.saveUserScore(params, userInfo));
	}

	@GetMapping("/delete")
	@ApiOperation(value="删除")
	public BaseResult delete(@RequestParam("id")Integer id) {
		UserScoreInfo entity = userScoreInfoService.getById(id);
		if(null == entity || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		userScoreInfoService.updateById(entity);
		behaviorCacheService.updateScoreCount(entity.getObjType(), entity.getObjNo(), -1);
		return BaseResult.ok();
	}

}
